import {useUserStore} from "@/store/modules/user.ts";

// 是否具有权限
export function hasPermission(need: Array<string> | string | undefined): boolean {
    // 防止ts神经病判断不了hasOwnProperty
    if(!need) return false;
    else if(typeof need === "string") {
        if(need === '*') return true;
        else return canSee([need]);
    } else {
        // * 匹配的是所有人
        if(need.indexOf("*") != -1) return true;
        else return canSee(need);
    }
}

// 数组类型判断是否存在某角色
function canSee(needRoles: Array<string>): boolean {
    const userStore = useUserStore();
    return needRoles.some(role => {
        return userStore.authorities && userStore.authorities.indexOf(role) != -1;
    })
}